package ejemplossql;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class PruebaBBDD
 */
@WebServlet("/PruebaBBDD")
public class PruebaBBDD extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public PruebaBBDD() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		AccesoBD bd = new AccesoBD();
		Connection con = bd.conectar();
		
		String query="select * from usuarios";
		Statement st = null;
		ResultSet rs = null;
		PrintWriter out = response.getWriter();
		response.setContentType("text/html");
		try {
			st = con.createStatement();
			rs = st.executeQuery(query);
			out.println("<table>");
			while(rs.next()){
				out.println("<tr>");
				out.println("<td>");
				out.println(rs.getString("Nif"));
				out.println("</td>");
				out.println("<td>");
				out.println(rs.getString("nombre"));
				out.println("</td>");
				out.println("<td>");
				out.println(rs.getString("Apellidos"));
				out.println("</td>");
				out.println("<td>");
				out.println(rs.getInt("edad"));
				out.println("</td>");
				out.println("<td>");
				out.println(rs.getDate("fechaalta"));
				out.println("</td>");
				out.println("/<tr>");
			}
			out.println("</table>");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		bd.cerrarConexion();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
